<!--添加采集器页面表单:
1、采集器设置表单
2、表单的验证
3、采集器的规则生成
4、采集器存储
-->
<style>
	.addpickerInput{width:500px;}
	label{width: 150px; float: left;}
	br{clear: left;}
	#addpickerSubmit{width:100px; height: 40px; margin-top: 20px; background-color: #111; color:#fff;}

</style>

<h3>添加采集器规则</h3>
<div>
	<form id="apForm" action="" method="POST">
		<input type="hidden" name="hidden">

		<h4>基本配置</h4>
		<label>名称</label><input class="addpickerInput" id="pick_name" type="text" name="pick_name" placeholder="采集器名称"><br>
		<label>网址</label><input class="addpickerInput" id="pick_url" type="text" name="pick_url" placeholder="网址,结尾不加/"><br>

		<h4>列表页配置</h4>
		<label>地址</label><input class="addpickerInput" id="list_url" type="text" name="list_url" placeholder="列表网址——页码用[*]代替"><br>
		<label>列表规则</label><input class="addpickerInput" id="list_tag" type="text" name="list_tag" placeholder="列表匹配规则——内容代替标签：MYCONTENT"><br>
		<label>翻页规则</label><input class="addpickerInput" id="list_page" type="text" name="list_page" placeholder="翻页匹配规则"><br>

		<h4>内容页配置</h4>
		<label>标题匹配规则</label><input class="addpickerInput" id="ctt_title" type="text" name="ctt_title" placeholder="标题匹配规则"><br>
		<label>正文匹配规则</label><input class="addpickerInput" id="ctt_content" type="text" name="ctt_content" placeholder="内容匹配规则"><br>

		<input id="addpickerSubmit" type="submit" name="submit">
	</form>
</div>

<?php 
	/**
	* 规则的处理
	*/
	// require_once( phy_path() . '/lib/wp-picker.php' );
	// $pker = new picker();

	if( isset($_POST['hidden']) ){
		#检验表单的正确性
		// $pker->checkFormValid( $_POST );

		//自动把表单中标识转化成正则表达式
		if( isset( $_POST['list_url'] ) && strlen( $_POST['list_url']) ){
			$list_tag = inputtopattern( $_POST['list_tag'] );	
			$list_page = inputtopattern( $_POST['list_page'] );
		}else{
			echo "<br>请正确输入列表信息<br>";
		}

		if(isset( $_POST['ctt_title']) && isset($_POST['ctt_content']) ){
			$content_title = inputtopattern( $_POST['ctt_title'] );
			$content_content = inputtopattern( $_POST['ctt_content'] );
		}else{
			echo "<br>请正确输入文章页信息<br>";
		}

		//存储采集器信息
		$data = datatoarray($_POST['pick_name'], $_POST['pick_url'], $_POST['list_url'], $list_tag, $list_page, $content_title, $content_content);
		$saveRel = save_picker($data);

		#检查表单的存储结果
		if( $saveRel ){
			echo "保存成功<br>";
		}else{
			echo "保存失败<br>";
		}

	}else{
		echo "SOMETHING WRONG!!!";
	}
	//////////////////////////////////////////////////////////////////////////////////
	/**
	* @name   datatoarray 把表单内容转化成数组
	* @param  string name url list list_tag list_page title content
	* @return array  data
	*/
	function datatoarray( $name, $url, $list, $list_tag, $list_page, $title, $content ){
		//把表单内容，转化成可存储的数组形式
		$data = array(
			'name'      => $name,
			'url'       => $url,
			'list'      => $list,
			'list_tag'  => $list_tag,
			'list_page' => $list_page,
			'title'     => $title,
			'content'   => $content,
			);
		return $data;
	}


	/**
	* @name   save_picker  存储采集器
	* @param  array data
	* @return boolean
	*/
	function save_picker( $data = array() ){
		global $wpdb;

		if( $wpdb->insert( 'wp_picker', $data ) == false ){
			return false;
		}else{
			return true;
		}
	}

	/**
	* @name   inputtopattern  把字符串规则转化为正则表达式规则
	* @param  string  str     字符串规则
	* @return string          正则表达式字符串
	*/
	function inputtopattern( $str = '' ){
		//将输入的标签转化成正则表达式
		$punc = array('+','*', '.', '[', ']', '\\', '\/', '?' , '(', ')', '{', '}', '^', '$', '&', '|', '"', "'");
		$a = '';
		for( $i=0 ; $i<mb_strlen($str) ; $i++ ){

			if( in_array($str[$i], $punc) ){
				#
				$a = $a . '\\' . $str[$i];
			}elseif( $str[$i] == ' ' || $str[$i]=='&nbsp;' ){
				$a = $a . '\s+';
			}elseif( $str[$i] == '/' ){
				$a = $a . '\\' . $str[$i];
			}else{
				$a = $a . $str[$i];
			}
		}
		$a = preg_replace( '/MYCONTENT/' , '([\w\s\W\S]*?)', $a );
		return "/".$a."/";	
	}

?>

